package com.feng.paper.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.common.consts.PaperConst;
import com.feng.common.enums.ApiErrorEnum;
import com.feng.common.exceptions.ServiceException;
import com.feng.common.vo.ApiRest;
import com.feng.paper.dto.SaveAnswerDto;
import com.feng.paper.entity.Paper;
import com.feng.paper.entity.PaperQu;
import com.feng.paper.entity.PaperQuAnswer;
import com.feng.paper.mapper.PaperMapper;
import com.feng.paper.mapper.PaperQuAnswerMapper;
import com.feng.paper.mapper.PaperQuMapper;
import com.feng.paper.service.PaperQuAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 试卷考题备选答案 服务实现类
 * </p>
 *
 * @author feng
 * @since 2023-03-23 20:30:42
 */
@Service
public class PaperQuAnswerServiceImpl extends ServiceImpl<PaperQuAnswerMapper, PaperQuAnswer> implements PaperQuAnswerService {
    @Autowired
    private PaperMapper paperMapper;

    @Autowired
    private PaperQuMapper paperQuMapper;

    @Autowired
    private PaperQuAnswerMapper paperQuAnswerMapper;

    @Override
    @Transactional
    public ApiRest saveAnswer(SaveAnswerDto saveAnswerDto) {
        if (saveAnswerDto == null) {
            throw new ServiceException("参数异常");
        }
        Paper paper = paperMapper.selectById(saveAnswerDto.getPaperId());
        if (paper == null) {
            throw new ServiceException(ApiErrorEnum.PAPER_NOT_EXIT);
        }
        if (StrUtil.isBlankIfStr(saveAnswerDto.getJdtval()) &&
                StrUtil.isBlankIfStr(saveAnswerDto.getRadioValue()) &&
                CollUtil.isEmpty(saveAnswerDto.getMultiValue())) {
            throw new ServiceException(ApiErrorEnum.PAPER_QU_ANSWER_NOT_EXIT);
        }
        //简答题
        if (!StrUtil.isBlankIfStr(saveAnswerDto.getJdtval())) {
            LambdaUpdateChainWrapper<PaperQu> lambdaUpdateChainWrapper
                    = new LambdaUpdateChainWrapper<>(paperQuMapper);
            return lambdaUpdateChainWrapper
                    .eq(PaperQu::getQuId, saveAnswerDto.getQuId())
                    .eq(PaperQu::getPaperId, saveAnswerDto.getPaperId())
                    .set(PaperQu::getAnswer, saveAnswerDto.getJdtval())
                    .set(PaperQu::getAnswered, PaperConst.ANSWERED)
                    .update() ?
                    ApiRest.success() : ApiRest.failure();
        }
        //客观题
        //每次更新客观题之前，将所有选项置空
        new LambdaUpdateChainWrapper<>(paperQuAnswerMapper)
                .eq(PaperQuAnswer::getPaperId, saveAnswerDto.getPaperId())
                .eq(PaperQuAnswer::getQuId, saveAnswerDto.getQuId())
                .set(PaperQuAnswer::getChecked, PaperConst.NOANSWERED)
                .update();
        //更新选项
        List<String> list = StrUtil.isBlankIfStr(saveAnswerDto.getRadioValue()) ?
                saveAnswerDto.getMultiValue() :
                Collections.singletonList(saveAnswerDto.getRadioValue());
        new LambdaUpdateChainWrapper<>(paperQuAnswerMapper)
                .in(PaperQuAnswer::getAnswerId, list)
                .eq(PaperQuAnswer::getPaperId, saveAnswerDto.getPaperId())
                .eq(PaperQuAnswer::getQuId, saveAnswerDto.getQuId())
                .set(PaperQuAnswer::getChecked, PaperConst.ANSWERED)
                .update();


        List<PaperQuAnswer> answerList = paperQuAnswerMapper.getAnswer(saveAnswerDto.getPaperId(), saveAnswerDto.getQuId());
        LambdaQueryChainWrapper<PaperQu> lambdaQueryChainWrapper
                = new LambdaQueryChainWrapper<>(paperQuMapper);
        PaperQu paperQu = lambdaQueryChainWrapper
                .eq(PaperQu::getQuId, saveAnswerDto.getQuId())
                .eq(PaperQu::getPaperId, saveAnswerDto.getPaperId())
                .one();
        if (CollUtil.isEmpty(answerList)) {
            //选项正确
            paperQu.setIsRight(PaperConst.RIGHT)
                    .setActualScore(paperQu.getScore())
                    .setAnswered(PaperConst.ANSWERED);

        } else {
            paperQu.setIsRight(PaperConst.WRONG)
                    .setActualScore(0)
                    .setAnswered(PaperConst.ANSWERED);
        }
        paperQuMapper.updateById(paperQu);
        return ApiRest.success();
    }
}
